import {microAppState} from "./MicroApp.state";
import {microAppBaseAssetsPublicPath} from "./MicroApp.baseAssetsPublicPath";
import {microAppHooks} from "./MicroApp.hooks";
import {microAppResource} from "./MicroApp.resource";
import {microAppTabs} from "./MicroApp.tabs";
import {microAppNav} from "./MicroApp.nav";
import {microAppPageList} from "./MicroApp.PageList";
import {microAppUsePageService} from "./MicroApp.usePageService";
import {microAppCreateLocalIndexConfig} from "./MicroApp.createLocalIndexConfig";
import {MicroAppModule} from "./MicroAppModule";
import {microAppRoute} from "./MicroApp.route";
import {microAppBaseRoutePublicPath} from "./MicroApp.baseRoutePublicPath";
import {microAppDefer} from "./MicroApp.defer";
import {MenuGroups} from "../menus";
import {delay} from "@peryl/react-compose";
import {MicroAppCache} from "./MicroApp.cache";
import {env} from "../env";
import {microAppLogin} from "./MicroApp.login";

export const createMicroApp = MicroAppModule.createOptionUser({
  enableCloseAllTabs: true,
  envName: env.envName,
  formatTabs: async (tabs) => {
    await delay(0);
    /*本来的目的是为了“这里通过菜单数据对标签数据进行格式化，主要是为了实现菜单多语言显示的功能”，现在实现标签多语言通过别的方式实现了，这里暂时没有作用了，可以考虑加载完菜单数据之后更新菜单的显示值，安全性等功能*/
    return tabs.map((tab) => {
      if (!tab.data?.menu) {
        return tab;
      }
      const menuId = tab.data.menu.id;
      const group = MenuGroups.id2map.menuId2group[menuId]!;
      const menu = MenuGroups.id2map.menuId2menu[menuId]!;
      return {
        ...tab,
        ...!menu ? {} : {
          label: menu.name,
          icon: group.icon,
          page: { path: menu.path, data: menu.data, },
        },
        pages: tab.pages,
      };
    });
  },
});

createMicroApp.registry(1, 'state', microAppState);
createMicroApp.registry(2, 'baseAssetsPublicPath', microAppBaseAssetsPublicPath);
createMicroApp.registry(3, 'baseRoutePublicPath', microAppBaseRoutePublicPath);
createMicroApp.registry(4, 'hooks', microAppHooks);
createMicroApp.registry(5, 'resource', microAppResource);
createMicroApp.registry(6, 'tabs', microAppTabs);
createMicroApp.registry(7, 'nav', microAppNav);
createMicroApp.registry(8, 'PageList', microAppPageList);
createMicroApp.registry(9, 'pageService', microAppUsePageService);
createMicroApp.registry(10, 'createLocalIndexConfig', microAppCreateLocalIndexConfig);
createMicroApp.registry(11, 'route', microAppRoute);
createMicroApp.registry(12, 'defer', microAppDefer);
createMicroApp.registry(13, 'cache', MicroAppCache);
createMicroApp.registry(14, 'login', microAppLogin);
